package net.messagenet.mazor.infra.mail;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import net.messagenet.mazor.exception.MZRInternalServerException;
import net.messagenet.mazor.infra.InfrastructureConstants;

import org.apache.log4j.Logger;




/**
 * This class is the stander implementation of the mail manager for the mazor system
 * it uses the stander java mail api and the mail session from the server.
 *
 * @author Idan
 */

public class MailManagerImpl implements MailManager {

	private Logger logger =  Logger.getLogger(this.getClass());
	private Session mail; 
	
	public MailManagerImpl() {
		
		logger.info("MailManager built ");
		
	}
	
	@Override
	public void sendDPASheetViaEmail(String sendToAddress,String cc,String filePath) throws MZRInternalServerException {
		sendMail(sendToAddress,cc,InfrastructureConstants.MAIL_DPA_SUBJECT,InfrastructureConstants.MAIL_DPA_CONTANT,filePath);
	}

	/**
	 * This method sends a mail with a file attaching
	 * 
	 * @param sendToAddress the address with the mail will be sent to
	 * @param subject the subject of the mail
	 * @param content the content of the message
	 * @param filePath the path of the file to be attached to the mail
	 * @param posRequestFilePath 
	 * @throws MZRInternalServerException 
	 */
	private void sendMail(String sendToAddress,String bcc,String subject,String content,String filePath) throws MZRInternalServerException{
		
		InitialContext ctx;
		try {
			ctx = new InitialContext();
	        mail = (Session) ctx.lookup(InfrastructureConstants.JAVA_MAIL_SERVICE_NAME); 

	        MimeMessage message = new MimeMessage(mail); 
	        message.setSubject(subject); 
	        message.setRecipients(Message.RecipientType.TO,   
	        		InternetAddress.parse(sendToAddress, false));
	        
	        
	        if (bcc!=null && !bcc.equals("")){
	        	if(bcc.indexOf(",")>0) {
                    message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(bcc));
                } else {
                    message.setRecipient(Message.RecipientType.CC, new InternetAddress(bcc));
                }

	        }
	        MimeBodyPart mbp1 = new MimeBodyPart();
	        mbp1.setText(content);
	        
	        MimeBodyPart mbp2 = new MimeBodyPart();

            // attach the file to the message
	        FileDataSource fds = new FileDataSource(filePath);
         	mbp2.setDataHandler(new DataHandler(fds));
         	mbp2.setFileName(fds.getName());

         	// create the Multipart and add its parts to it
         	Multipart mp = new MimeMultipart();
         	mp.addBodyPart(mbp1);
         	mp.addBodyPart(mbp2);

         	// add the Multipart to the message
         	message.setContent(mp);
	        
	        Transport.send(message); 
		} catch (NamingException e) {
			throw new MZRInternalServerException("poblem while getting the mail session", e);
		} catch (MessagingException e) {
			throw new MZRInternalServerException("poblem in mail sending", e);

		} 
	}

	@Override
	public void sendPOSSheetViaEmail(String email, String cc,
			String posRequestFilePath) throws MZRInternalServerException {
		sendMail(email,cc,InfrastructureConstants.MAIL_POS_SUBJECT,InfrastructureConstants.MAIL_POS_CONTANT,posRequestFilePath);
		
	}

}
